package data;

import java.util.Calendar;
import java.util.Date;

import org.jboss.netty.buffer.ChannelBuffer;

import util.ChannelBufferUtil;

import com.mongodb.BasicDBObject;

public class RecordItem {
	public BasicDBObject db;
	public boolean noDate = false;
	
	public RecordItem(BasicDBObject db) {
		this.db = db;
	}
	
	public Date getDate() {
		Date date = null;
		if (db.containsField("date")) {
			date = (Date)db.get("date");
		} else {
			date = Calendar.getInstance().getTime();
			db.put("date", date);
			noDate = true;
		}		
		return date;
	}
	
	public int isChallenger() {
		if (db.containsField("isChallenger")) {
			return db.getInt("isChallenger");
		} else {
			return 1;
		}
	}

	public void setDate(Date date) {
		db.put("date", date);
	}
	
	public void setChallenger(int v) {
		db.put("isChallenger", v);
	}
	
	public String getName() {
		return db.getString("name"); 
	}
	
	public String getGUID() {
		return db.getString("guid");
	}
	
	public int getLevel() {
		return db.getInt("level");
	}
	
	public int getSex() {
		return db.getInt("sex");
	}
	
	public boolean isWin() {
		return db.getBoolean("win");
	}
	
	public void setName(String v) {
		db.put("name", v); 
	}
	
	public void setGUID(String v) {
		db.put("guid", v);
	}
	
	public void setLevel(int v) {
		db.put("level", v);
	}
	
	public void setSex(int v) {
		db.put("sex", v);
	}
	
	public void setWin(boolean v) {
		db.put("win", v);
	}
	
	public void serialize(ChannelBuffer buf) {
		ChannelBufferUtil.writeString(buf, getName());
		ChannelBufferUtil.writeString(buf, getGUID());
		buf.writeInt(getLevel());
		buf.writeByte(getSex());
		ChannelBufferUtil.writeBool(buf, isWin());
	}
}
